PrepareTitleFirstTime:	

	;; code to do for title only on first time it is shown

	;; we want to fade in the title screen initially
	bsr	FadeOutFake

	;; set demo level to level 0
	clr.b	DemoLevel
	;; set flags to two player so when changed,
	;; first demo will be one player
	bset	#TwoPlayerFlag,Flags
	;; skip actual fade out on the first time
	bra.s	PrepareTitleAlreadyFadeOut

PrepareTitleFadeOut:	
	bsr	FadeOut
PrepareTitleAlreadyFadeOut:	
	;; switch two player for demo
	bchg	#TwoPlayerFlag,Flags

	;; clear HScroll cell
	move.l	#$5C000003,d7
	bsr	ClearHScroll32

	;; clear plane A VRAM C000
	move.l  #$40000003,d7
	bsr     ClearPlane64By28In64By32
	;; clear plane B VRAM E000
	move.l  #$60000003,d7
	bsr     ClearPlane64By28In64By32

	;; copy (compressed) title tiles over VRAM 2000
        move.l   #$60000000,(a4)
        lea      StartBANNERTiles(pc),a5
        move.w   #((EndBANNERTiles-StartBANNERTiles)/2-1),d5
TitleTileCopy:
        move.w   (a5)+,d0
	move.w	 d0,d1
	andi.w   #$3333,d0
	move.w   d0,(a3)
	lsr.w	 #2,d1
	andi.w   #$3333,d1
	move.w   d1,(a3)
        dbf      d5,TitleTileCopy

	;; copy title map with border
	lea     StartBANNERMap(pc),a5
	move.w  #$0100,d4
        moveq   #(BannerTileWidth-1),d5
        moveq   #(BannerTileHeight-1),d6
	;; VRAM C210 (plane A)
	move.l	#$42100003,d7
	bsr     CopyMapAAndMakeBorderB

	;; copy title last line map
	lea     StartTitleLastLine(pc),a5
	;; d4[8-15] already cleared: clr.w   d4
        moveq   #(StartRowTitleLastLine-StartTitleLastLine-1),d5
        moveq   #(((EndTitleLastLine-StartTitleLastLine)/(StartRowTitleLastLine-StartTitleLastLine))-1),d6
	;; VRAM CD20
	move.l	#$4D200003,d7
	bsr     CopyMap

	;; initialize sprites
        lea      StartSpriteTitleInitial(pc),a5
	bsr	 SpriteLoad

	;; reset vint wait
	clr.w	VintWait
	;; reset score to 000 for both players
	;; this will reset between each demo and start of game
	move.l	#$30303030,StartScoreAscii
	move.w	#$3030,StartScoreAscii+4

	;; set title/demo/game state
	bclr	#DemoFlag,Flags

	;; reset back to title truck animation
	lea	TitleTruckAnimateMove(pc),a5
	move.l	a5,VintAnimate

	btst	#StartButtonFlag,Flags
	bne	PrepareStart

	;; copy title player map
	lea     StartTitlePushStartButton(pc),a5
	clr.w   d4
        moveq   #(StartRowTitlePushStartButton-StartTitlePushStartButton-1),d5
        moveq   #(((EndTitlePushStartButton-StartTitlePushStartButton)/(StartRowTitlePushStartButton-StartTitlePushStartButton))-1),d6
	;; VRAM CA96
	move.l	#$4A960003,d7
	bsr     CopyMap

	bsr	FadeIn

	;; d2 has cheat code index for port1
	moveq	#0,d2
	;; d3 has cheat code index for port2
	moveq	#0,d3
	;; our level select code
	lea	LevelSelectCode(pc),a5
	
TitleLoop:
        bsr     WaitForVint
	move.b	Port1Press,d0
	move.b	Port2Press,d1
	btst	#bitStart,d0
	bne.s   PrepareStart
	btst	#bitStart,d1
	bne.s   PrepareStart
	;; test port 1 for 1 player code
	;; only check if a button is pressed
	tst.b	d0
	beq.s	TitleSkipPort1Press
	;; determine current button for code
	move.b	(a5,d2.w),d4
	cmp.b	d0,d4
	bne.b	TitleResetPort1Code
	;; increment index for code
	addq	#1,d2
	;; determine if end of code (if zero)
	move.b	(a5,d2.w),d4
	tst.b	d4
	bne.s	TitleSkipPort1Press
	;; start one player level select
	bclr	#TwoPlayerFlag,Flags
	bra	PrepareLevelSelect
TitleResetPort1Code:
	moveq	#0,d2
TitleSkipPort1Press:	
	;; test port 2 for 2 player code
	;; only check if a button is pressed
	tst.b	d1
	beq.s	TitleSkipPort2Press
	;; determine current button for code
	move.b	(a5,d3.w),d4
	cmp.b	d1,d4
	bne.b	TitleResetPort2Code
	;; increment index for code
	addq	#1,d3
	;; determine if end of code (if zero)
	move.b	(a5,d3.w),d4
	tst.b	d4
	bne.s	TitleSkipPort2Press
	;; start two player level select
	bset	#TwoPlayerFlag,Flags
	bra	PrepareLevelSelect
TitleResetPort2Code:
	moveq	#0,d3
TitleSkipPort2Press:	

	;; title only shown for a certain number of frames before going to demo
	cmpi.w	#VintWaitTitle,VintWait
	blt.s	TitleLoop
	bset	#DemoFlag,Flags
	
	;; determine current demo level
	clr.w	d0
	move.b	DemoLevel,d0
	;; increment demo level for next time if two player
	btst	#TwoPlayerFlag,Flags
	beq.s	TitleSkipDemoLevelIncrement
	move.b	d0,d1
	addq.b	#1,d1
	;; if level is beyond max, reset to level 0
	cmpi.b	#(((EndLevelDemoJumpTable-StartLevelSelectJumpTable)/(StartRowLevelSelectJumpTable-StartLevelSelectJumpTable))-1),d1
	ble.s	TitleSkipDemoLevelReset
	moveq	#0,d1
TitleSkipDemoLevelReset:	
	;; save new incremented level for next demo
	move.b	d1,DemoLevel
TitleSkipDemoLevelIncrement:	
	;; final select our level for demo
	bra	LevelSelectJump

PrepareStart:	
	;; copy title player map
	lea     StartTitlePlayer12(pc),a5
	clr.w   d4
        moveq   #(StartRowTitlePlayer12-StartTitlePlayer12-1),d5
        moveq   #(((EndTitlePlayer12-StartTitlePlayer12)/(StartRowTitlePlayer12-StartTitlePlayer12))-1),d6
	;; 24x12 12x6
	;; VRAM CA16
	move.l	#$4A160003,d7
	bsr     CopyMap

	;; make border on player 1
	clr.w   d4
	;; subtract an extra 3 for the added whitespace
        moveq   #(StartRowTitlePlayer12-StartTitlePlayer12-4),d5
        clr.w   d6
	;; VRAM E998
	move.l	#$69980003,d7
	bsr     MakeBorder

	bsr	FadeIn

StartLoop:	
        bsr     WaitForVint
	move.b	Port1Press,d0
	move.b	Port2Press,d1
	;; d2/d3 now has SACBRLDU (pressed)
	btst    #bitStart,d0
	bne	PrepareLevel0
	btst	#TwoPlayerFlag,Flags
	beq.s	StartSkipTwoPlayerStartCheck
	btst    #bitStart,d1
	bne	PrepareLevel0
StartSkipTwoPlayerStartCheck:	
	btst    #bitUp,d0
	bne.s	TitlePlayer1
	btst    #bitDown,d0
	bne.s	TitlePlayer2
	btst    #bitUp,d1
	bne.s	TitlePlayer1
	btst    #bitDown,d1
	bne.s	TitlePlayer2
	bra.s	StartLoop

TitlePlayer1:	
	bclr	#TwoPlayerFlag,Flags

	;; clear border on player 2
	;; subtract an extra 3 for the added whitespace
        moveq   #(StartRowTitlePlayer12-StartTitlePlayer12-2),d5
        moveq   #2,d6
	;; VRAM EA98
	move.l	#$6A980003,d7
	bsr     ClearMap

	;; make border on player 1
	;; VRAM E998
	move.l	#$69980003,d7
	bra.s   CommonForBothTitlePlayers	

TitlePlayer2:	
	bset	#TwoPlayerFlag,Flags

	;; clear border on player 1
	;; subtract an extra 3 for the added whitespace
        moveq   #(StartRowTitlePlayer12-StartTitlePlayer12-2),d5
        moveq   #2,d6
	;; VRAM E998
	move.l	#$69980003,d7
	bsr     ClearMap

	;; make border on player 2
	;; VRAM EA98
	move.l	#$6A980003,d7
CommonForBothTitlePlayers:	
	clr.w   d4
	;; subtract an extra 3 for the added whitespace
        moveq   #(StartRowTitlePlayer12-StartTitlePlayer12-4),d5
        clr.w   d6
	bsr     MakeBorder
EndStartLoop:
	bra.s   StartLoop


;;; data used for title

	
StartTitlePushStartButton:
	dc.b "PUSH START BUTTON"
StartRowTitlePushStartButton:
EndTitlePushStartButton:

;; has leading spaces and empty middle line to clear PUSH START BUTTON text
StartTitlePlayer12:
	dc.b "  1 PLAYER START "
StartRowTitlePlayer12:
	dc.b "                 "
	dc.b "  2 PLAYER START "
EndTitlePlayer12:
	
StartTitleLastLine:	
	dc.b "2010  BV"
StartRowTitleLastLine:
EndTitleLastLine:	

LevelSelectCode:
	dc.b btnC,btnA,btnB,$00

StartSpriteTitleInitial:
RileySpriteTitleInitial:
	dc.b $01,$2C,$06,$01,$48,$80,$01,$E0
TruckLeftSpriteTitleInitial:
	dc.b $01,$40,$0F,$02,$00,$AD,$00,$01
TruckRightSpriteTitleInitial:
	dc.b $01,$40,$0B,$00,$00,$BD,$00,$21
EndSpriteTitleInitial:	
	
	include BANNER.ASM
